"""
接 "pca_ForeignBlog",本身不能运行
"""
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import time

# %% == 奇异值SVD分解 ================================================================
from sklearn.decomposition import TruncatedSVD

# svd没有transform方法, 很容易理解, svd是求特定某个矩阵的奇异值,怎么能transform到其他矩阵上呢
svd = TruncatedSVD(n_components=3, random_state=42).fit_transform(df_zs)

plt.figure(figsize=(12, 8))
plt.title('SVD Components')
# 看三个特征值之间是否有关系(有一定的线性关系)
plt.scatter(svd[:, 0], svd[:, 1])
plt.scatter(svd[:, 1], svd[:, 2])
plt.scatter(svd[:, 2], svd[:, 0])
plt.show()

# %% == ICA 独立成分分析 ================================================================
from sklearn.decomposition import FastICA

t1 = time.time()
ica_fit = FastICA(n_components=3, random_state=12).fit(df_zs)
ica = ica_fit.predict(df_zs)

plt.figure(figsize=(12, 8))
plt.title('ICA Components')
plt.scatter(ica[:, 0], ica[:, 1])
plt.scatter(ica[:, 1], ica[:, 2])
plt.scatter(ica[:, 2], ica[:, 0])
plt.show()

print("ICA 耗时: %.2f s" % (time.time() - t1))    # 0.35s

# %% == Isomap ================================================================
from sklearn import manifold

'''
n_neighbors决定每个点的邻居数
n_components决定流形的坐标数
n_jobs  = -1 将使用所有可用的 CPU 内核
'''
t1 = time.time()
iso_fit = manifold.Isomap(n_neighbors=5, n_components=3, n_jobs=-1).fit(df_zs)
iso = iso_fit.predict(df_zs)

plt.figure(figsize=(12, 8))
plt.title('Decomposition using ISOMAP')
plt.scatter(iso[:, 0], iso[:, 1])
plt.scatter(iso[:, 1], iso[:, 2])
plt.scatter(iso[:, 2], iso[:, 0])
plt.show()

print("Isomap 耗时: %.2f s" % (time.time() - t1))  # 24.59s

# %% == t-SNE ================================================================
from sklearn.manifold import TSNE

t1 = time.time()
tsne = TSNE(n_components=3, n_iter=300).fit_transform(df_zs)

plt.figure(figsize=(12, 8))
plt.title('t-SNE components')
plt.scatter(tsne[:, 0], tsne[:, 1])
plt.scatter(tsne[:, 1], tsne[:, 2])
plt.scatter(tsne[:, 2], tsne[:, 0])
plt.show()

print("t-SNE 耗时: %.2f s" % (time.time() - t1))  # 26.75s

# %% == UMAP ================================================================
import umap  # 需要 umap_learn 包

t1 = time.time()
umap_fit = umap.UMAP(n_neighbors=5, min_dist=0.3, n_components=3).fit(df_zs)
um = umap_fit.predict(df_zs)

plt.figure(figsize=(12, 8))
plt.title('Decomposition using UMAP')
plt.scatter(um[:, 0], um[:, 1])
plt.scatter(um[:, 1], um[:, 2])
plt.scatter(um[:, 2], um[:, 0])
plt.show()

print("UMAP 耗时: %.2f s" % (time.time() - t1))  # 13.75s
